package com.itany.shop.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author:石小俊
 * @Date: 2022/12/22 9:07
 * @Version:1.0-SNAPSHOT
 * @Description:
 */
public class LogFilter implements Filter {

    private FilterConfig config;

    @Override
    public void init(FilterConfig config) throws ServletException {
        this.config = config;
//        String username = config.getInitParameter("username");
//        String password = config.getInitParameter("password");
//        System.out.println("username:"+username+",password:"+password);
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        // 日志操作,记录谁(ip)在什么时间访问了什么请求(uri)
        String ip = request.getRemoteAddr();
        String uri = request.getRequestURI();
        System.out.println(ip+"在"+new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss").format(new Date())+"访问了"+uri+"请求");

        String username = config.getInitParameter("username");
        String password = config.getInitParameter("password");
        System.out.println("username:"+username+",password:"+password);
        // 放行
         filterChain.doFilter(servletRequest,servletResponse);
    }

    @Override
    public void destroy() {

    }
}
